package com.zinnaworks.smart_cloud_gamepad;

import com.zinnaworks.smart_cloud_gamepad.popup.ExitDialogPopup;
import com.zinnaworks.smart_cloud_gamepad.popup.ExitDialogPopup.myOnClickListener;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ConnectGamepad extends CoreServiceActivity {

	private ImageView connectButton;
	
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(android.os.Build.VERSION.SDK_INT > 9){
			ThreadPolicy policy = new ThreadPolicy.Builder().permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}

	}

	@Override
	protected void onServiceAvailable(CoreService coreService) {

		if (!getKeyStoreManager().hasServerIdentityAlias()) {
			setContentView(R.layout.activity_connectgamepad);

			
			connectButton = (ImageView)findViewById(R.id.connectButton);
			connectButton.setEnabled(false);
			connectButton.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View paramView) {
					showMainActivity(true);
				}
			});

			KeystoreInitializerTask kit = new KeystoreInitializerTask(getUniqueId());
			kit.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getKeyStoreManager());
		}
		else {
			showMainActivity(false);
		}
	}
	
	private void showMainActivity(boolean isBoolean){
		Intent intent = new Intent(ConnectGamepad.this, MainActivity.class);
		intent.putExtra("SHOW_PASSWORKD_VIEW", isBoolean);
		startActivity(intent);
	}

	@Override
	protected void onServiceDisconnecting(CoreService coreService) {
		// TODO Auto-generated method stub

	}

	private class KeystoreInitializerTask extends AsyncTask<
	KeyStoreManager, Void, Void> {
		private final String id;

		public KeystoreInitializerTask(String id) {
			this.id = id;
		}

		@Override
		protected Void doInBackground(KeyStoreManager... keyStoreManagers) {

			if (keyStoreManagers.length != 1) {
				throw new IllegalStateException("Only one key store manager expected");
			}
			keyStoreManagers[0].initializeKeyStore(id);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			connectButton.setEnabled(true);
		}
	}

	private String getUniqueId() {
		String id = Settings.Secure.getString(getContentResolver(),
				Settings.Secure.ANDROID_ID);
		// null ANDROID_ID is possible on emulator
		return id != null ? id : "emulator";
	}
	
	myOnClickListener myListener;
	ExitDialogPopup exit;
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {

		//하드웨어 뒤로가기 버튼에 따른 이벤트 설정
		case KeyEvent.KEYCODE_BACK:
			myListener = new myOnClickListener() {
				@Override
				public void onButtonClick(boolean bl) {
					if(bl)
						android.os.Process.killProcess(android.os.Process.myPid());
					else
						exit.cancel();
					
				}
			};
			exit = new ExitDialogPopup(this,myListener);
			exit.show();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}


}
